const { DataTypes } = require('sequelize')
const sequelize = require('../db/connection')
const User = require('./User')

const Device = sequelize.define('Device', {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    name: {
        type: DataTypes.STRING,
        allowNull: false
    },
    type: {
        type: DataTypes.STRING,
        allowNull: false
    },
    location: {
        type: DataTypes.STRING,
        allowNull: false
    },
    status: {
        type: DataTypes.BOOLEAN,
        defaultValue: false
    }
})

// 设置外键关联
Device.belongsTo(User, {
    foreignKey: 'userId',
    as: 'user'
})

User.hasMany(Device, {
    foreignKey: 'userId',
    as: 'devices'
})

module.exports = Device 